package StructuralPattern.Proxy.DynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @Classname : GamePlayerHandler
 * @Description : 代理处理器
 * @Author : chentianyu
 * @Date 2023/2/12 22:18
 */


public class GamePlayerHandler implements InvocationHandler {

    private GamePlayer gamePlayer;

    public GamePlayerHandler(GamePlayer gamePlayer) {
        this.gamePlayer = gamePlayer;
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object obj = null;

        // 前置处理
        before(proxy, method, args);

        // 执行具体角色方法
        obj = method.invoke(this.gamePlayer, args);

        // 后置处理
        after(proxy, method, args);

        return obj;
    }

    private void before(Object proxy, Method method, Object[] args) {

    }

    private void after(Object proxy, Method method, Object[] args) {
        if ("upgrade".equals(method.getName())) {
            System.out.println("每升一级计费100元。");
        }
    }
}
